package com.powergisol.gis.controller;

import com.powergisol.gis.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * Date: 2019/7/24  16:02
 * Description:
 */
@RestController
@RequestMapping("file")
@Api(description = "上传" ,value = "上传")
public class UploadController {

    @PostMapping("/upload")
    @ApiOperation(value = "图片上传返回base64", notes = "图片上传返回base64")
    public Result formUpload(MultipartFile file){
        Result result = new Result();
        try {
            if(file != null && !file.isEmpty()){
                InputStream in = file.getInputStream();
                byte[] buffer = new byte[(int) file.getSize()];
                in.read(buffer);
                in.close();
                String str =  Base64.encodeBase64String(buffer);
                result.setData(str);
            }else {
                result.failure(404,"请传入文件");
            }
        }catch (Exception ex){
            ex.printStackTrace();
            result.failure(-3,ex.getMessage());
        }
        return result;
    }
}
